package com.holly.unit.gen.starter;

import com.holly.unit.gen.api.GenerateCodeApi;
import com.holly.unit.gen.api.config.GenCodeCfg;
import com.holly.unit.gen.api.pojo.TableInfo;
import com.holly.unit.gen.starter.autoconf.prop.GenCodeExtProperties;
import com.holly.unit.genx.GenerateCodeOperator;
import lombok.Builder;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

/**
 * 类描述: 代码生成器类
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/1/19 14:40
 */
@Builder
public class AutoGenerator {


    private  final GenerateCodeApi generateCodeApi;

    private GenCodeExtProperties properties;


    public AutoGenerator(GenerateCodeApi generateCodeApi,GenCodeExtProperties properties) {
        GenCodeCfg cfg = new GenCodeCfg();
        BeanUtils.copyProperties(properties,cfg);
        this.generateCodeApi = new GenerateCodeOperator(cfg);
        this.properties = properties;
    }


    public AutoGenSection gen() {
        Assert.notNull(generateCodeApi, "代码生成器服务未注入，请确认配置正确");
        return new AutoGenSection();
    }

    public class AutoGenSection {

        public void code(TableInfo info, String customerPath) {
            try {
                generateCodeApi.autoGen(info,customerPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void code(TableInfo tableInfo,String path,String template,String customerPath) {
            try {
                generateCodeApi.autoGen(tableInfo,path,template,customerPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
